package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaProcedure;
import com.wudl.govern.service.MetaProcedureService;
import com.wudl.govern.vo.MetaProcedureVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * 函数，存储过程表 Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaProcedure")
public class MetaProcedureController {

    @Autowired
    private MetaProcedureService metaProcedureService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaProcedureVO>> page(@RequestBody MetaProcedureVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaProcedureVO> MetaProcedureVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaProcedure> MetaProcedureList = metaProcedureService.page(page, size);

        for (MetaProcedure metaProcedureTmp:MetaProcedureList )
        {
            MetaProcedureVO metaProcedureVO = new MetaProcedureVO();
            BeanUtil.copyProperties(metaProcedureTmp, MetaProcedureVO.class);
            MetaProcedureVoList.add(metaProcedureVO);
        }
        return CommonResult.success(MetaProcedureVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaProcedureVO> get(@PathVariable Object id) {
        MetaProcedure entity = metaProcedureService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaProcedureVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaProcedureVO addVO) {
        metaProcedureService.save(BeanUtil.copyProperties(addVO, MetaProcedure.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaProcedureVO editVO) {
        metaProcedureService.updateById(BeanUtil.copyProperties(editVO, MetaProcedure.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Object id) {
        metaProcedureService.removeById(id);
        return CommonResult.success();
    }

}
